package com.atguigu.gmallcanal;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

public class MyProducer {

    private static Producer<String,String> producer;

    static{
        producer = getProducer();
    }

    private static Producer<String,String> getProducer(){
        //ProducerConfig查找卡夫卡生产者常用配置,序列化类自己搜
        Properties properties = new Properties();
        //必传的三个配置
        properties.setProperty("bootstrap.servers","hadoop102:9092,hadoop103:9092");
        properties.setProperty("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        properties.setProperty("value.serializer","org.apache.kafka.common.serialization.StringSerializer");
        KafkaProducer producer = new KafkaProducer(properties);

        return producer;
    }

    public static void writeToKafka(String topic,String jsonstr){

        ProducerRecord<String,String> producerRecord = new ProducerRecord<>(topic,jsonstr);
        producer.send(producerRecord);
    }

}
